//
//  NextBozhuViewController.m
//  Ting
//
//  Created by 安静的美男子 on 14/11/24.
//  Copyright (c) 2014年 安静的美男子. All rights reserved.
//

#import "NextBozhuViewController.h"
#import "BozhuMainViewController.h"
@interface NextBozhuViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation NextBozhuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.DataSource = [NSMutableArray array];
    
    _type = @"hot";
    
    [self getData];
    [self creatTable];
    
    self.mbview = [[MBProgressHUD alloc] initWithView:self.view];
    self.mbview.labelText = @"正在努力加载中...";
    [self.mbview setMode:MBProgressHUDModeIndeterminate];
    [self.mbview show:YES];
    [self.view addSubview:self.mbview];
    

    [self CreatTabbar];
    
    
    
    // Do any additional setup after loading the view.
}
-(void)creatTable
{
    _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 568 - 44) style:UITableViewStyleGrouped];
    
    [self.view addSubview:_table];
    _table.dataSource = self;
    _table.delegate = self;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *celll = @"bozhuNext";
    BozhuNextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:celll];
    if (!cell) {
        cell = [[BozhuNextTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:celll];
        
    }
    [cell setmodel:_DataSource[indexPath.section]];
    
    return cell;
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  _DataSource.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_DataSource.count != 0) {
        
        return 1;
    }
    return 0;
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 150;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
    
}
-(void)getData
{
    
    GetData *data = [[GetData alloc]init];
    [data getDataURL:[NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_user_list?category_name=%@&condition=%@&device=iPhone&page=1&per_page=20",_name,_type] dic:nil block:^(id result) {
        
        NSArray *arr =[ result objectForKey:@"list"];
        for (NSDictionary *dic in arr) {
            BozhuNextModel *model = [[BozhuNextModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.DataSource addObject:model];
            
            NSLog(@"%@",model.nickname);
            
        }
        [self.mbview removeFromSuperview];

        [_table reloadData];
    }];
    
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BozhuMainViewController *vc = [[BozhuMainViewController alloc]init];
    vc.Uid = [_DataSource[indexPath.section] uid];
    [self.navigationController pushViewController:vc animated:YES];
    
}
-(void)CreatTabbar
{
    UIView *downView = [[UIView alloc]initWithFrame:CGRectMake(-1, [UIScreen mainScreen].bounds.size.height - 50, [UIScreen mainScreen].bounds.size.width + 2, 51)];
    downView.backgroundColor = [UIColor blackColor];
    downView.alpha = 1;
    downView.layer.borderWidth = 1;
    downView.layer.borderColor =[[UIColor grayColor ]CGColor];;
    
    [self.view addSubview:downView];
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(10, 15, 24, 24);
    
    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:@"iconfont-fanhui.png"] forState:UIControlStateNormal];
    [downView addSubview:backButton];
    
    

    
    UIView *black = [[UIView alloc]initWithFrame:CGRectMake(64 * 2, 568 - 64, 64, 64)];
    [self.view addSubview:black];
    black.layer.cornerRadius = 32;
    black.clipsToBounds = YES;
    
    black.backgroundColor = [UIColor blackColor];
    
    self.play = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.play.frame = CGRectMake(0, 0, 60, 60);
    self.play.center = black.center;
    
    [self.view addSubview:self.play];
    
    self.play.layer.cornerRadius = 30;
    [self.view bringSubviewToFront:self.play];
    self.play.clipsToBounds = YES;
    [self.play addTarget:self action:@selector(GotoPlay) forControlEvents:UIControlEventTouchUpInside];
    
    [self.play sd_setBackgroundImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.Playmodel.coverLarge]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"123.png"]];
    
    
    
}
-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
    AudioStreamerManager *audioManager = [AudioStreamerManager shareInstanceManager];
    _Playmodel = audioManager.Mymodel;
    
    
    if (audioManager.flag) {
        
        if (!_Timer) {
            //            self.Timer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(zhuan) userInfo:nil repeats:YES];
            
            self.Timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(zhuan) userInfo:nil repeats:YES];
        }
        
        //将定时器加入循环
        //循环用于系统页面响应,定时器和进程
        //                [[NSRunLoop currentRunLoop] addTimer:_Timer forMode:NSDefaultRunLoopMode];
        
        
        [_Timer setFireDate:[NSDate distantPast]];
        
        
        
        
    }
    else
    {
        
        [_Timer setFireDate:[NSDate distantFuture]];
        
        //        if ([_Timer isValid]) {
        //            永久停止
        //            [self.Timer invalidate];
        //            _Timer =nil;
        //        }
        
        
    }
    
}
-(void)zhuan
{
    self.play.transform = CGAffineTransformRotate(self.play.transform,2 * M_PI / 60.0 /100);
    //    if (_model.coverLarge) {
    [self.play sd_setBackgroundImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.Playmodel.coverLarge]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"123.png"]];
    //    }
}

-(void)GotoPlay
{
    PlayViewController *vc = [[PlayViewController alloc]init];
    AudioStreamerManager *audioManager = [AudioStreamerManager shareInstanceManager];
    vc.model = audioManager.Mymodel;
    vc.DataSource = audioManager.Mydata;
    /**
     *  number不记录
     */
    
    //    vc.number = audioManager.num;
    vc.num = [NSString stringWithFormat:@"%ld",audioManager.num];
    
    
    [self presentViewController:vc animated:YES completion:nil];
    
}
-(void)viewDidDisappear:(BOOL)animated
{
    [self.Timer invalidate];
    _Timer =nil;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
